home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Environments / AppMkr151#2 / Libraries / MPW / AMLibraryC / WindowAids.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-27  |  10.5 KB  |  485 lines  |  [TEXT/MPS ]

  1. /* © 1988-91, Bowers Development Corp. */
  2. /* WindowAids.c */
  3.  
  4. #include <Types.h>
  5. #include <Quickdraw.h>
  6. #include <Controls.h>
  7. #include <Dialogs.h>
  8. #include <Events.h>
  9. #include <Fonts.h>
  10. #include <Lists.h>
  11. #include <Menus.h>
  12. #include <Resources.h>
  13. #include <ToolUtils.h>
  14.  
  15. #include "Globals.h"
  16.  
  17. #include "WindowAids.h"
  18.  
  19. short        textJust;        /* set by SetWFont */
  20.  
  21. #define topLeft(r)    (((Point *) &(r))[0])
  22.  
  23. #pragma segment WindowAids
  24.     
  25. typedef struct {
  26.         Handle            reserved;
  27.         Rect            displayRect;
  28.         Byte            itemKind;
  29.         Byte            dataLen;
  30. } ItemTemplate, *ItemTPtr;
  31.  
  32. #define ItemTBaseSize    sizeof (ItemTemplate)
  33.  
  34. typedef struct {
  35.         short            maxItem;
  36.         ItemTemplate    items;    /* repeated */
  37. } WitlTemplate, *WitlTPtr, **WitlTHndl;
  38.  
  39. /*----------*/
  40. void    GetWRect        (short            itemNr,
  41.                          Rect            *itemRect)
  42. {
  43.     WitlTHndl        witlHandle;
  44.     ItemTPtr        wItemPtr;
  45.     short            I;
  46.     short            dataSize;
  47.  
  48.     SetRect (itemRect, 0, 0, 16, 16);
  49.     witlHandle = (WitlTHndl) cur->witlHandle;
  50.     if ((witlHandle != NULL)
  51.     &&  (itemNr <= (**witlHandle).maxItem + 1) ) {
  52.         wItemPtr = &(**witlHandle).items;
  53.         for (I = 1; I <= itemNr - 1; I++) {
  54.             dataSize = wItemPtr->dataLen + (wItemPtr->dataLen & 1);
  55.             wItemPtr = (ItemTPtr) (((Ptr)wItemPtr) + (ItemTBaseSize + dataSize));
  56.         } /*for*/
  57.         *itemRect = wItemPtr->displayRect;
  58.     }
  59. } /* GetWRect */
  60.     
  61. /*----------*/
  62. typedef struct {
  63.         short            header;
  64.         short            offset;
  65. } ictbEntry, *ictbEntryPtr;
  66.  
  67. typedef struct {
  68.         ictbEntry        item [5000];
  69. } WictTemplate, *WictTPtr, **WictTHndl;
  70.  
  71. typedef struct {
  72.         short            fontNum;
  73.         Style            typeStyle;
  74.         SignedByte        just;        /* AppMaker adition */
  75.         short            typeSize;
  76.         RGBColor        fgColor;
  77.         RGBColor        bgColor;
  78.         short            xferMode;
  79. } StyleData, *StyleDataPtr;
  80.  
  81. /* defined in TextEdit: */
  82. /*    doFont            = 1;        set font (family) number */
  83. /*    doFace            = 2;        set character style */
  84. /*    doSize            = 4;        set type size */
  85. /*    doColor            = 8;        set color */
  86. /*    addSize            = 16;        adjust type size */
  87.  
  88. /* set background color */
  89. #define doBgColor    0x2000
  90.  
  91. /* set transfer mode */
  92. #define doXferMode    0x4000
  93.  
  94. /* set font by name */
  95. #define doByName    0x8000
  96.  
  97. /*----------*/
  98. void    SetWFont        (short            itemNr)
  99. {
  100.     WictTHndl        wictHandle;
  101.     Ptr                wictPtr;
  102.     ictbEntryPtr    itemPtr;
  103.     short            header;
  104.     short            offset;
  105.     StyleDataPtr    dataPtr;
  106.     StyleData        data;
  107.     StringPtr        fontPtr;
  108.     Str255            fontName;
  109.  
  110.     wictHandle = (WictTHndl) cur->wictHandle;
  111.     if (wictHandle != nil) {
  112.         wictPtr = (Ptr) &(**wictHandle);
  113.         itemPtr = &(**wictHandle).item [itemNr - 1];
  114.         header = itemPtr->header;
  115.         offset = itemPtr->offset;
  116.         dataPtr = (StyleDataPtr) (wictPtr + offset);
  117.         data = *dataPtr;
  118.         if ((header & doFont) == 0) {
  119.             data.fontNum = 0;
  120.         } else {
  121.             if ((header & doByName) != 0) {
  122.                 fontPtr = (StringPtr) (wictPtr + data.fontNum);
  123.                 BlockMove (fontPtr, &fontName, 256);
  124.                 GetFNum (fontName, &data.fontNum);
  125.             }
  126.         }
  127.         if ((header & doFace) == 0) {
  128.             data.typeStyle = 0;
  129.         }
  130.         if ((header & doSize) == 0) {
  131.             data.typeSize = 0;
  132.         }
  133.         if ((header & doXferMode) == 0) {
  134.             data.xferMode = srcCopy;
  135.         }
  136.         TextFont (data.fontNum);
  137.         TextFace (data.typeStyle);
  138.         TextMode (data.xferMode);
  139.         TextSize (data.typeSize);
  140.         textJust = data.just;
  141.     }
  142. } /* SetWFont */
  143.     
  144. /*----------*/
  145. WindowPtr GetWindow        (short            windowID)
  146. {
  147.     if (sysConfig.hasColorQD) {
  148.         return (GetNewCWindow (windowID, NULL, (WindowPtr) -1L));
  149.     } else {
  150.         return (GetNewWindow (windowID, NULL, (WindowPtr) -1L));
  151.     }
  152. } /* GetWindow */
  153.     
  154. /*----------*/
  155. ListHandle NewV1List    (Rect            bounds,
  156.                           WindowPtr        parentWindow)
  157. {
  158.     Rect            listBounds;
  159.     Rect            dataBounds;
  160.     Point            cSize;
  161.     ListHandle        list;
  162.  
  163.     listBounds = bounds;
  164.     listBounds.right = listBounds.right - 15;    /*for scroll bar*/
  165.     SetRect (&dataBounds, 0, 0, 1, 0);    /*one column, no rows*/
  166.     SetPt (&cSize, listBounds.right - listBounds.left, 0);
  167.     list = LNew    (&listBounds,    /*dialog item's box*/
  168.                  &dataBounds,    /*one column, no rows*/
  169.                  cSize,            /*cell size: full width, standard height*/
  170.                  0,                /*procid - standard text list*/
  171.                  parentWindow,    /*parent window*/
  172.                  false,            /*draw it*/
  173.                  false,            /*has no grow*/
  174.                  false,            /*no horizontal scroll*/
  175.                  true);            /*vertical scroll*/
  176.     (**list).selFlags = lOnlyOne + lNoNilHilite;
  177.     return (list);
  178. } /*NewV1List*/
  179.  
  180. /*----------*/
  181. Boolean    GetListChoice    (short            *choice,
  182.                          ListHandle        list)
  183. {
  184.     Boolean            result;
  185.     Cell            selectedCell;
  186.  
  187.     SetPt (&selectedCell, 0, 0);
  188.     if (LGetSelect (true, &selectedCell, list)) {
  189.         *choice = selectedCell.v;
  190.         result = true;
  191.     } else {
  192.         *choice = -1;
  193.         result = false;
  194.     }
  195.  
  196.     return (result);
  197. } /*GetListChoice*/
  198.  
  199. /*----------*/
  200. void    SetListChoice    (short            choice,
  201.                          ListHandle        list)
  202. {
  203.     Cell            selectedCell;
  204.  
  205.     SetPt (&selectedCell, 0, choice);
  206.     LSetSelect (true, selectedCell, list);
  207. } /*SetListChoice*/
  208.  
  209. /*----------*/
  210. void    GetListRow        (Str255            data,
  211.                          short            index,
  212.                          ListHandle        list)
  213. {
  214.     Cell            selectedCell;
  215.     short            dataLen;
  216.  
  217.     SetPt (&selectedCell, 0, index);
  218.     dataLen = 255;        /*var parameter to LGetCell*/
  219.     LGetCell (&data [1], &dataLen, selectedCell, list);
  220.     data [0] = (char) dataLen;
  221. } /*GetListRow*/
  222.  
  223. /*----------*/
  224. void    SetListRow        (Str255            data,
  225.                          short            index,
  226.                          ListHandle        list)
  227. {
  228.     Cell            selectedCell;
  229.  
  230.     SetPt (&selectedCell, 0, index);
  231.     LSetCell (&data [1], data [0], selectedCell, list);
  232. } /*SetListRow*/
  233.  
  234. /*----------*/
  235. void AddToList        (Str255            data,
  236.                      ListHandle        list)
  237. {
  238.     short            newRow;
  239.     
  240.     #define maxint    32767
  241.  
  242.     newRow = LAddRow (1, maxint, list);
  243.     SetListRow (data, newRow, list);
  244. } /*AddToList*/
  245.  
  246. /*----------*/
  247. void DrawList    (ListHandle        list)
  248. {
  249.     PenState        savePen;
  250.     Rect            frame;
  251.  
  252.     GetPenState (&savePen);
  253.     PenNormal ();
  254.     frame = (**list).rView;
  255.     InsetRect (&frame, -1, -1);
  256.     FrameRect (&frame);
  257.     SetPenState (&savePen);
  258.     LUpdate ((**list).port->visRgn, list);
  259. } /*DrawList*/
  260.  
  261. /*----------*/
  262. void TextIDBox        (short            textID,
  263.                      Rect            bounds)
  264. {
  265.     Handle            text;
  266.  
  267.     text = GetResource ('TEXT', textID);
  268.     if (text != NULL) {
  269.         LoadResource (text);
  270.         HLock (text);
  271.         TextBox (&(**text), GetHandleSize (text), &bounds, textJust);
  272.         HUnlock (text);
  273.     }
  274. } /*TextIDBox*/
  275.  
  276. /*----------*/
  277. void PlotIconID        (short            iconID,
  278.                      Rect            bounds)
  279. {
  280.     Handle            icon;
  281.  
  282.     icon = GetIcon (iconID);
  283.     if (icon != NULL) {
  284.         LoadResource (icon);
  285.         PlotIcon (&bounds, icon);
  286.     }
  287. } /*PlotIconID*/
  288.  
  289. /*----------*/
  290. void DrawPictureID    (short            pictID,
  291.                      Rect            bounds)
  292. {
  293.     PicHandle        pict;
  294.  
  295.     pict = GetPicture (pictID);
  296.     if (pict != NULL) {
  297.         LoadResource ((Handle) (pict));
  298.         DrawPicture (pict, &bounds);
  299.     }
  300. } /*DrawPictureID*/
  301.  
  302. /*----------*/
  303. void DrawGrayLine    (Rect            bounds)
  304. {
  305.     PenState        savePen;
  306.  
  307.     GetPenState (&savePen);
  308.     PenNormal ();
  309.     PenPat (&qd.gray);
  310.     MoveTo (bounds.left, bounds.top);
  311.     LineTo (bounds.right - 1, bounds.bottom - 1);
  312.     SetPenState (&savePen);
  313. } /*DrawGrayLine*/
  314.  
  315. /*----------*/
  316. void    DrawTriangle (const Rect     *bounds);
  317. void    DrawTriangle (const Rect     *bounds)
  318. {
  319.     PolyHandle        arrowTriangle;
  320.  
  321.     arrowTriangle = OpenPoly ();
  322.     MoveTo (bounds->right - 15, bounds->top + 5);
  323.     Line (5, 5);
  324.     Line (5, -5);
  325.     Line (-10, 0);
  326.     ClosePoly ();
  327.     FillPoly (arrowTriangle, &qd.black);
  328.     KillPoly (arrowTriangle);
  329. } /*DrawTriangle*/
  330.  
  331. /*----------*/
  332. void UpdatePopup    (Rect            bounds,
  333.                      short            menuID,
  334.                      short            choice)
  335. {
  336.     MenuHandle        menu;
  337.     Str255            menuItem;
  338.     Rect            textRect;
  339.  
  340.     menu = (MenuHandle) (GetResource ('MENU', menuID));
  341.     if (menu != NULL) {
  342.         GetItem (menu, choice, menuItem);
  343.         textRect = bounds;
  344.         textRect.left = textRect.left + 12;
  345.         textRect.right = textRect.right + 20;
  346.         TextBox (&menuItem [1], menuItem [0], &textRect, teJustLeft);
  347.     }
  348.  
  349.     DrawTriangle (&bounds);
  350.  
  351.     bounds.right  = bounds.right  - 1;
  352.     bounds.bottom = bounds.bottom - 1;
  353.     FrameRect (&bounds);
  354.     MoveTo (bounds.right, bounds.top + 2);
  355.     LineTo (bounds.right, bounds.bottom);
  356.     LineTo (bounds.left + 2, bounds.bottom);
  357. } /*UpdatePopup*/
  358.  
  359. /*----------*/
  360. void TrackPopup        (Rect            bounds,
  361.                      short            menuID,
  362.                      short            *choice)
  363. {
  364.     MenuHandle        menu;
  365.     Point            corner;
  366.     long            result;
  367.     Rect            triangleRect;
  368.  
  369.     menu = (MenuHandle) (GetResource ('MENU', menuID));
  370.     if (menu != NULL) {
  371.         InsertMenu (menu, -1);
  372.         triangleRect.top = bounds.top + 5;
  373.         triangleRect.left = bounds.right - 18;
  374.         triangleRect.bottom = triangleRect.top + 7;
  375.         triangleRect.right = triangleRect.left + 13;
  376.         EraseRect (&triangleRect);
  377.         corner = topLeft (bounds);
  378.         LocalToGlobal (&corner);
  379.         CheckItem (menu, *choice, true);
  380.         result = PopUpMenuSelect (menu, corner.v, corner.h + 1, *choice);
  381.         CheckItem (menu, *choice, false);
  382.         DeleteMenu (menuID);
  383.         InvalRect (&triangleRect);            /*because we always erase triangleRect*/
  384.         if ((HiWord (result) != 0) && (LoWord (result) != *choice)) {
  385.             *choice = LoWord (result);
  386.             InsetRect (&bounds, 1, 1);
  387.             InvalRect (&bounds);
  388.         }
  389.     }
  390. } /*TrackPopup*/
  391.  
  392. /*----------*/
  393. Boolean TrackButton        (ControlHandle    button,
  394.                          Point            mousePos)
  395. {
  396.     return (TrackControl (button, mousePos, NULL) != 0);
  397. } /*TrackButton*/
  398.  
  399. /*----------*/
  400. void TrackCheck        (ControlHandle    checkBox,
  401.                      Point            mousePos,
  402.                      Boolean        *checked)
  403. {
  404.     if (TrackControl (checkBox, mousePos, NULL) != 0) {
  405.         *checked = !*checked;
  406.         SetCtlValue (checkBox, *checked);
  407.     }
  408. } /*TrackCheck*/
  409.  
  410. /*----------*/
  411. void TrackRadio        (ControlHandle    radio,
  412.                      Point            mousePos,
  413.                      short            *choice)
  414. {
  415.     long            refCon;
  416.     short            group;
  417.     ControlHandle    control;
  418.  
  419.     if (TrackControl (radio, mousePos, NULL) != 0) {
  420.         refCon = GetCRefCon (radio);
  421.         group = HiWord (refCon);
  422.         *choice = LoWord (refCon);
  423.         control = ((WindowPeek) qd.thePort)->controlList;
  424.         while (control != NULL) {
  425.             if ((**control).contrlDefProc == (**radio).contrlDefProc) {
  426.                 refCon = GetCRefCon (control);
  427.                 if (HiWord (refCon) == group) {
  428.                     SetCtlValue (control, 0);
  429.                 }
  430.             }
  431.             control = (**control).nextControl;
  432.         } /*while*/
  433.         SetCtlValue (radio, 1);
  434.     }
  435. } /*TrackRadio*/
  436.  
  437. /*----------*/
  438. void TrackMulti        (ControlHandle    multi,
  439.                      Point            mousePos,
  440.                      short            *value)
  441. {
  442.     if (TrackControl (multi, mousePos, NULL) != 0) {
  443.         if (*value == GetCtlMax (multi)) {
  444.             *value = GetCtlMin (multi);
  445.         } else {
  446.             (*value)++;
  447.         }
  448.         SetCtlValue (multi, *value);
  449.     }
  450. } /*TrackMulti*/
  451.  
  452. /*----------*/
  453. void TrackPalette    (ControlHandle    palette,
  454.                      Point            mousePos,
  455.                      short            *choice)
  456. {
  457.     if (TrackControl (palette, mousePos, NULL) != 0) {
  458.         *choice = GetCtlValue (palette);
  459.     }
  460. } /*TrackPalette*/
  461.  
  462. /*----------*/
  463. void    EnableControl    (ControlHandle    theControl,
  464.                          Boolean        active)
  465. {
  466.     if (theControl != NULL) {
  467.         if (active) {
  468.             HiliteControl (theControl, 0);
  469.         } else {
  470.             HiliteControl (theControl, 255);
  471.         }
  472.     }
  473. } /*EnableControl*/
  474.  
  475. /*----------*/
  476. /* for backwards compatibility:*/
  477. /*----------*/
  478. void    HiliteScroll    (ControlHandle    scroll,
  479.                          Boolean        active)
  480. {
  481.     EnableControl (scroll, active);
  482. } /*HiliteScroll*/
  483.  
  484. /* WindowAids */
  485.